# -*- coding: utf-8 -*-
import scrapy
from food.items import FoodItem


class CateSpider(scrapy.Spider):
    name = 'cate'
    allowed_domains = ['www.meishij.net']
    start_urls = ['https://www.meishij.net/chufang/diy/']

    def parse(self, response):
        # 1, 定位所有数据
        note_list = response.xpath("//*[@id='listtyle1_list']/div")
        # print(len(note_list))
        # 2, 遍历提取的数据
        for note in note_list:
            item = FoodItem()
            item['name'] = note.xpath("./a/div/div/div/strong/text()").extract_first()
            item['comment'] = note.xpath("./a/div/div/div/span/text()").extract_first()
            item['come'] = note.xpath("./a/div/div/div/em/text()").extract_first()
            item['link'] = note.xpath("./a/@href").extract_first()
            item['img_link'] = note.xpath("./a/img/@src").extract_first()
            # print(item)
            yield item
            # 3, 翻页
        next_url = response.xpath("//*[@class='next']/@href").extract_first()
        # print(next_url)
        print('page'+str(next_url.split('=')[-1]))
        if next_url:
            yield scrapy.Request(next_url, callback=self.parse)
        pass
